package com.example.servlet;

import com.example.DAO.DBUtilsDAO;
import com.example.DAO.UserDao;
import com.example.DAO.impl.DBUtilsFactory;
import com.example.DAO.impl.UserDaoImpI;
import com.example.pojo.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet({"/login"})
public class LoginServlet extends HttpServlet {
    public LoginServlet() {
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = null;
        String usernameFromIndex = req.getParameter("username");
        String passwordFromIndex = req.getParameter("password");


                if (usernameFromIndex.isEmpty() && passwordFromIndex.isEmpty()) {
                session = req.getSession();
                session.setAttribute("nullUsername", "用户名为空！");
                session.setAttribute("nullPassword", "密码为空！");
                req.getRequestDispatcher("/login.jsp").forward(req, resp);
            } else if (usernameFromIndex.isEmpty()) {
                session = req.getSession();
                session.setAttribute("nullUsername", "用户名为空！");
                req.getRequestDispatcher("/login.jsp").forward(req, resp);
            } else if (passwordFromIndex.isEmpty()) {
                session = req.getSession();
                session.setAttribute("nullPassword", "密码为空！");
                req.getRequestDispatcher("/login.jsp").forward(req, resp);
            }


        UserDao userDao = new UserDaoImpI();
        User user1 = userDao.findByUserName(usernameFromIndex);
        String password = user1.getPassword();
        if (user1 != null) {

            if (user1.getPassword().equals(passwordFromIndex)) {
                User user = new User(user1.getUsername(), password, user1.getIdentity());
                HttpSession session2 = req.getSession();
                session2.setAttribute("user", user);
                Cookie userName = new Cookie("username", user1.getUsername());
                Cookie userPass = new Cookie("password", password);
                Cookie identity = new Cookie("identity", user1.getIdentity());
                userName.setMaxAge(86400);
                userPass.setMaxAge(86400);
                identity.setMaxAge(86400);
                resp.addCookie(userName);
                resp.addCookie(userPass);
                resp.addCookie(identity);
                if ("管理员".equals(user1.getIdentity())) {
                    req.getRequestDispatcher("/home.jsp").forward(req, resp);
                    session2.setAttribute("warn", user1.getIdentity());
                } else {
                    req.getRequestDispatcher("/customer.jsp").forward(req, resp);
                }
            }
        }

    }
}
